package com.example.finallyjob.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.example.finallyjob.dao.ActivityImp;
import com.example.finallyjob.dao.IndexImp;
import com.example.finallyjob.entity.*;
import com.mongodb.client.result.UpdateResult;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.bson.BsonValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Date;
import java.util.UUID;


@Service
public class Comment {
    Logger logger = LoggerFactory.getLogger(Comment.class);
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    ActivityImp activityImp;
    @Autowired
    ArticleMake articleMake;


    public String NewRely(String name, String num, String text,
                          String title, String type, Integer auth) throws JSONException {
        String id= UUID.randomUUID().toString();
        Reply reply=new Reply(id,num,name,text);
        Reply insert = mongoTemplate.insert(reply);
        if (insert != null) {
            Date date=new Date();
            Info info=new Info(name,"5",num,title,type,
                    new Timestamp(new Date(date.getYear(),date.getMonth(),
                    date.getDate()+1,00,00,00).getTime()),auth);
            Info insert1 = mongoTemplate.insert(info);
            if(insert1 != null) {
                return insert.getComment_id();
            }else {
                return null;
            }
        }else {
            return null;
        }
    }


    public String comment(String num, String text, String name, int auth,
                          String title, String type, String replyList) throws JSONException {
        String id=UUID.randomUUID().toString();
        System.out.println(replyList);
        Comments comments=new Comments(id,num,name,text);
        Comments insert = mongoTemplate.insert(comments);
        JSONArray json = null;
        if (insert != null) {
            if (replyList == null || replyList.equals("") || replyList.equals("null")) {
                json = new JSONArray();
                json.add(id);
            } else {
                System.out.println("json="+replyList);
                json = JSONArray.parseArray(replyList);
                json.add(id);
            }
        }
        Query query=new Query(new Criteria("comment_id").is(num));
        Update update=new Update();
        String list=json.toJSONString();
        update.set("comment_list",list);
        UpdateResult upsert = mongoTemplate.upsert(query, update, Reply.class);
        long matchedCount = upsert.getMatchedCount();
        if(matchedCount > 0){
            Date date=new Date();
            Info info=new Info(name,"4",num,title,
                    type,new Timestamp(new Date(date.getYear(),date.getMonth(),
                    date.getDate()).getTime()),auth);
            Info insert1 = mongoTemplate.insert(info);
            if(insert1 != null) {
                return list;
            }else {
                return null;
            }
        }else {
            return null;
        }
    }

    public String follow(Integer id,String name, String num, String friend, String title,
                         String type, int friendid, String fname){
        JSONObject jsonObject;
        System.out.println(id);
        if(friend == null || friend.equals("") || friend.equals("null")){
            jsonObject=new JSONObject();
        }else{
            friend=friend.replace("\\","");
            jsonObject=JSON.parseObject(friend);
        }
        jsonObject.put(String.valueOf(friendid),fname);
        friend=jsonObject.toString();
        Query query=new Query(new Criteria("userId").is(id));
        Update update=new Update();
        update.set("friend",friend);
        UpdateResult result=mongoTemplate.upsert(query,update, User.class);
        System.out.println(result.toString());
        Date date=new Date();
        Info info=new Info(name,"1",num,title,type,
                new Timestamp(new Date(date.getYear(),date.getMonth(),
                date.getDate()+1,00,00,00).getTime()),
                friendid);
        mongoTemplate.insert(info);
        return friend;
    }


    @Transactional(rollbackFor = Exception.class)
    public Boolean warning(String article, int auth, String name,
                           String title, String Type) throws Exception {
        Article articles=new Article();
        articles.setType(Type);
        articles.setArticleNum(article);
        Integer towarning = articleMake.Towarning(articles);
        if( towarning > 0) {
            Date date = new Date();
            Info info = new Info(name, "2", article, title, Type,
                    new Timestamp(new Date(date.getYear(), date.getMonth(),
                            date.getDate() + 1, 00, 00, 00).getTime()), auth);
            Info insert = mongoTemplate.insert(info);
            if(insert != null) {
                return true;
            }else {
                return false;
            }
        }else {
            return false;
        }

    }

    @Transactional(rollbackFor = Exception.class)
    public Integer likes(String article, String name, int auth,
                         String title, String type) throws Exception {
        Article articles=new Article();
        articles.setType(type);
        articles.setArticleNum(article);
        Integer i=articleMake.Toagree(articles);
        if(i > 0) {
            Date date=new Date();
            Info info=new Info(name,"3",article,title,type,
                    new Timestamp(new Date(date.getYear(),date.getMonth(),
                            date.getDate()).getTime()),auth);
            Info insert = mongoTemplate.insert(info);
            if(insert != null) {
                return i;
            }else {
                return i;
            }
        }else {
            return i;
        }
    }


}
